﻿var text_input = "Text";
var adv_mode = "UBB Code - Prompt before Insert\n\nClick on the button will display the propmt window objID will guide you to insert UBB Code";
var normal_mode = "UBB Code - Insert Immediately\n\nClick on the button will insert the UBB Code immediately";
var email_normal = "Please input the display text of the Email Link.\nLeave blank will use the Email Link itself.";
var email_normal_input = "Please input the Email Link.";
var fontsize_normal = "Please input the text objID will use this size.";
var font_normal = "Please input the text objID will use this font.";
var bold_normal = "Please input the text objID will use bold style.";
var italicize_normal = "Please input the text objID will use italic style.";
var underline_normal = "Please input the text objID will use underline style.";
var strike_normal = "Please input the text objID will use strike-line style.";
var sup_normal = "Please input the text objID will use superscript style.";
var sub_normal = "Please input the text objID will use subscript style.";
var quote_normal = "Please input the quoted text.";
var color_normal = "Please input the text objID will use this color.";
var center_normal = "Please input the text objID you want to align center.";
var link_normal = "Please input the display text of the URL.\nLeave blank will use the URL itself.";
var link_normal_input = "Please input the URL.";
var image_normal = "Please input the URL of the image file.";
var media_type = "Please input the media type. swf=Flash, wmp=Windows Media Player, rm=RealPlayer, qt=QuickTime.";
var media_size = "Please input the media screen size(Width,Height like 400,300)\nLeave blank will using default size.";
var media_url = "Please input the URL of the media source.";
var code_normal = "Please input the code segment.";
var list_normal = "Please input the list items. Leave blank to end input.";
var seperator_normal = "This Seperator is used for manually dividing Excerpt & Further Text Content.\nWithout inserting the Seperator, the Text Content will be seperated automatically.\nOnly One seperator is allowed in Text Content.";

var defmode = "advmode";

if (defmode == "advmode") {
    normalmode = false;
    advmode = true;
}
else {
    normalmode = true;
    advmode = false;
}
function setfocus(){
    document.form1.message.focus();
}

function chmode(swtch){
    if (swtch == 1) {
        normalmode = false;
        advmode = true;
        alert(normal_mode);
    }
    else 
        if (swtch == 0) {
            advmode = false;
            normalmode = true;
            alert(adv_mode);
        }
}

function AddText(NewCode){
    if (document.all) {
        insertAtCaret(document.form1.message, NewCode);
        setfocus();
    }
    else {
        document.form1.message.value += NewCode;
        setfocus();
    }
}

function storeCaret(cursorPosition){
    if (cursorPosition.createTextRange) 
        cursorPosition.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret(textEl, text){
    if (textEl.createTextRange && textEl.caretPos) {
        var caretPos = textEl.caretPos;
        caretPos.text += caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
    }
    else 
        if (textEl) {
            textEl.value += text;
        }
        else {
            textEl.value = text;
        }
}

function commonUbb(ubb, title, ubbtitle){
    var startUbb, endUbb;
    if (ubbtitle != null) {
        startUbb = "[" + ubb + "=" + ubbtitle + "]"
    }
    else {
        startUbb = "[" + ubb + "]";
    }
    endUbb = "[/" + ubb + "]";
    if (document.selection && document.selection.type == "Text") {
        var range = document.selection.createRange();
        range.text = startUbb + range.text + endUbb;
    }
    else 
        if (advmode) {
            AddTxt = startUbb + endUbb;
            AddText(AddTxt);
        }
        else {
            if (title == null) 
                title = text_input;
            txt = prompt(sup_normal, text_input);
            if (txt != null) {
                AddTxt = startUbb + txt;
                AddText(AddTxt);
                AddText(endUbb);
            }
        }
}

function chsize(size){
    commonUbb("size", text_input, size);
}

function chfont(font){
    commonUbb("font", text_input, font);
}

function bold(){
    commonUbb("b");
}

function italicize(){
    commonUbb("i");
}

function underline(){
    commonUbb("u");
}

function strike(){
    commonUbb("s");
}

function superscript(){
    commonUbb("sup");
}

function subscript(){
    commonUbb("sub");
}

function chcolor(color){
    commonUbb("color", commonUbb, color);
}

function center(){
    commonUbb("align=center");
}

function hyperlink(){
    commonUbb("url", "http://");
}

function email(){
    commonUbb("email");
}

function image(){
    commonUbb("img");
}

function code(){
    commonUbb("code");
}

function quote(){
    commonUbb("quote");
}

function media(){
    txt = prompt(media_type, "swf");
    while ("swf,wmp,rm,qt".indexOf(txt) < 0 || txt == "") {
        txt = prompt(media_type, "swf");
    }
    txt1 = prompt(media_size, "");
    txt2 = prompt(media_url, "http://");
    if (txt != null && txt2 != null) {
        if (txt1 == "" || txt1 == null) {
            AddTxt = "\n[" + txt + "]" + txt2;
        }
        else {
            AddTxt = "\n[" + txt + "=" + txt1 + "]" + txt2;
        }
        AddText(AddTxt);
        AddText("[/" + txt + "]");
    }
}

function list(){
    if (advmode) {
        AddTxt = "\n[list]\n[*]\n[*]\n[*]\n[/list]\n";
        AddText(AddTxt);
    }
    else {
        AddTxt = "\n[list]\n";
        txt = "1";
        while ((txt != "") && (txt != null)) {
            txt = prompt(list_normal, "");
            if (txt != "") {
                AddTxt += "[*]" + txt + "\n";
            }
        }
        AddTxt += "[/list]\n";
        AddText(AddTxt);
    }
}

function insertSmilies(strCode){
    var txtarea = document.form1.message;
    strCode = ' ' + strCode + ' ';
    if (txtarea.createTextRange && txtarea.caretPos) {
        var caretPos = txtarea.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? strCode + ' ' : strCode;
        txtarea.focus();
    }
    else {
        txtarea.value += strCode;
        txtarea.focus();
    }
}

function seperator(){
    var txtarea = document.form1.message;
    alert(seperator_normal);
    if (txtarea.createTextRange && txtarea.caretPos) {
        var caretPos = txtarea.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? '[separator] ' : '[separator]';
        txtarea.focus();
    }
    else {
        txtarea.value += '[separator]';
        txtarea.focus();
    }
}
